package com.qst.express.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qst.commonUtils.express.RandomUtil;
import com.qst.commonUtils.result.R;

import com.qst.express.entity.Express;
import com.qst.express.entity.User;
import com.qst.express.entity.vo.ExpressVo;
import com.qst.express.service.ExpressService;
import com.qst.express.utils.AddressUtil;
import com.qst.express.utils.SMSUtil;
import com.qst.express.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mjh
 * @since 2022-09-25
 */
@Api(description = "快件管理")
@RestController
@RequestMapping("/e/express")
@CrossOrigin
public class ExpressController {
    @Autowired
    ExpressService expressService;

    /**
     * TODO 条件查询带分页
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "快递分页查询")
    @PostMapping("/pageTeacherCondition/{page}/{limit}")
    public R pageTeacherCondition(@PathVariable("page") Integer page,
                                  @PathVariable("limit") Integer limit,
                                  @RequestBody(required = false) ExpressVo expressVo,HttpServletRequest request) {
        String adminPhone = UserUtil.getAdminPhone(request.getSession());
        System.out.println("管理员手机号："+adminPhone);
        System.out.println("管理员名字："+request.getSession().getAttribute("adminName"));
        // 获取用户手机号码
//        User wxUser = UserUtil.getWxUser(request.getSession());
//        String userphone = wxUser.getUphone();
//        System.out.println("wx管理员手机号："+userphone);
        Page<Express> expressVoPage = new Page<>(page, limit);
        IPage<Express> teacherIPage = expressService.pageTeacherCondition(expressVoPage, expressVo);
        return R.ok().data("total", teacherIPage.getTotal()).data("rows", teacherIPage.getRecords());
    }

    /**
     * 用于查询数据库中的全部快递（总数+新增），待取件快递（总数+新增）
     * @return [{size:总数,day:新增},{size:待取件数,day:今日新增}]
     */
    @ApiOperation(value = "快递控制台数据")
    @GetMapping("/console")
    public R console() {
        List<Map<String, Integer>> data = expressService.console();
        return R.ok().data("expressTotal", data);
    }
    /**
     * 按月查询
     * @return
     */
    @ApiOperation(value = "按月查询")
    @GetMapping("/console-column")
    public R consoleColumn() {
        List<Map<String, Integer>> data = expressService.consoleMonth();
        return R.ok().data("expressMonth", data);
    }


    /**
     * 添加快递
     * @param express
     * @return
     */
    @ApiOperation(value = "添加快递")
    @PostMapping("/addExpress")
    public R addExpress(@RequestBody(required = false) Express express, HttpServletRequest request) {
        System.out.println(express);
        // 获取管理或快递员的手机号码
        String adminPhone = UserUtil.getAdminPhone(request.getSession());
        express.setSysphone(adminPhone);
        System.out.println(adminPhone);
        System.out.println(request.getSession().getAttribute("adminName"));
        // 生成取件码
        String code = RandomUtil.getCode();
        express.setCode(code);
        express.setIntime(new Date());
        boolean save = expressService.save(express);
        if (save) {
            String address = AddressUtil.getAddress();
            SMSUtil.send(express.getUserphone(),code,address,adminPhone);
            return R.ok();
        }else {
            return R.error();
        }
    }
    /**
     * 删除快递
     * @param expressId
     * @return
     */
    @ApiOperation(value = "删除快递")
    @DeleteMapping("/deleteExpressId/{id}")
    public R removeExpressId(@PathVariable(value = "id") String expressId){
        boolean remove = expressService.removeById(Integer.parseInt(expressId));
        if (remove) {
            return R.ok();
        }else {
            return R.error();
        }
    }
    /**
     * 批量删除快递
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除快递")
    @PostMapping("/deletes")
    public R deltes(@RequestBody(required = false) Integer[] ids) {
        System.out.println(ids);
        boolean remove = false;
        for (Integer id : ids) {
            remove = expressService.removeById(id);
        }
        if (remove) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    /**
     * 查询快递信息同id
     * @param expressId
     * @return
     */
    @ApiOperation(value = "查询快递信息同id")
    @GetMapping("/getExpressInfo/{id}")
    public R findExpressInfoById(@PathVariable(value = "id") String expressId){
        Express express = expressService.getById(expressId);
        if (express != null) {
            return R.ok().data("express",express);
        }else {
            return R.error();
        }
    }

    /**
     * 更新快递信息
     * @param express
     * @return
     */
    @ApiOperation(value = "更新快递信息")
    @PutMapping("/updateExpressInfo")
    public R updateExpressInfo(@RequestBody(required = false) Express express){
        boolean update = expressService.updateById(express);
        if (update) {
            return R.ok();
        }else {
            return R.error();
        }
    }


}

